<template>
  <el-dialog
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    title="下载模板"
    width="600px"
    @close="handleClose"
  >
    <div class="download-dialog-content">
      <!-- 物料内容选择 -->
      <el-descriptions :column="1" border class="custom-descriptions">
        <el-descriptions-item label="*物料内容">
          <el-form-item prop="materialContent" style="margin-top: 15px;">
            <el-tree-select
              v-model="selectedMaterials"
              :data="materialTreeData"
              placeholder="请选择"
              :props="treeSelectProps"
              filterable
              allow-clear
              show-checkbox
              check-strictly
              :checkable="node => isThirdLevelNode(node)"
              style="width: 100%;"
              multiple
              collapse-tags
              @change="handleMaterialChange"
            />
          </el-form-item>
        </el-descriptions-item>
      </el-descriptions>
      
      <!-- 提示文本 -->
      <div class="tip-text">
        可选择一个或多个物料（最多5个），下载时打成zip下载，内容为excel表
      </div>
    </div>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleDownload" :disabled="selectedMaterials.length === 0">下载</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

// 定义props
const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  materialTreeData: {
    type: Array,
    required: true
  }
})

// 定义emits
const emit = defineEmits(['close', 'download', 'update:visible', 'materialChange'])

// 已选择的物料（存储value集合）
const selectedMaterials = ref([])

// 树形选择配置
const treeSelectProps = {
  value: 'value',
  label: 'label',
  children: 'children'
}

// 判断是否为第三级节点
const isThirdLevelNode = (node) => {
  return node.level === 3
}

// 处理物料选择变化
const handleMaterialChange = (values) => {
  // 触发事件，将选择的value集合传递给父组件
  emit('materialChange', values)
}

// 处理关闭
const handleClose = () => {
  emit('update:visible', false)
  emit('close')
  // 重置选择
  selectedMaterials.value = []
}

// 处理下载
const handleDownload = () => {
  if (selectedMaterials.value.length === 0) {
    ElMessage.warning('请至少选择一个物料')
    return
  }
  
  if (selectedMaterials.value.length > 5) {
    ElMessage.warning('最多只能选择5个物料')
    return
  }
  
  // 传递最终选择的value集合给父组件
  emit('download', selectedMaterials.value)
  emit('update:visible', false)
  selectedMaterials.value = []
}
</script>

<style scoped>
.download-dialog-content {
  padding: 10px 0;
}

/* 从index.vue迁移相关样式 */
.custom-descriptions {
  width: 100%;
}

.custom-descriptions .el-descriptions__item {
  display: flex;
  border-bottom: 1px solid #EBEEF5;
}

.custom-descriptions .el-descriptions__label {
  width: 120px;
  font-weight: 500;
  color: #606266;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.custom-descriptions .el-descriptions__content {
  flex: 1;
  color: #303133;
  display: flex;
  align-items: center;
  padding: 18px 10px 0 10px !important;
}

.custom-descriptions .el-descriptions__row:last-child .el-descriptions__item {
  border-bottom: none;
}

.el-descriptions__cell--label {
  background-color: transparent !important;
}

.tip-text {
  margin-top: 15px;
  color: #ff4d4f;
  font-size: 14px;
  line-height: 1.5;
}
.el-form-item {
  margin-bottom: 11px;
}
</style>